﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Interface.Api;

namespace yafc.SessionLayer
{
	public class Session
	{
		#region Fields

		private LinkedList<Account> accounts = new LinkedList<Account>();
		bool activated = false;

		#endregion

		#region Public Properties

		public bool Activated
		{
			get
				{ return activated; }
			set
				{ activated = value; }
		}

		public IEnumerable<Post> NewPosts
		{
			get
			{
				if (!activated)
				{
					throw new Exception("Inactivated session use AllPosts instead!");
				}

				List<Post> result = new List<Post>();
				lock (accounts)
				{
					foreach (Account acc in accounts)
					{
						result.AddRange(acc.NewFrndPosts);
					}
				}
				return result.OrderBy(p => p.dateTime);
			}
		}

		public IEnumerable<Post> OldPosts
		{
			get
			{
				List<Post> result = new List<Post>();
				lock (accounts)
				{
					foreach (Account acc in accounts)
					{
						result.AddRange(acc.OldFrndPosts);
					}
				}
				return result.OrderBy(p => p.dateTime);
			}
		}

		#endregion

		#region Public Methods

		public bool contains(Account Acc)
		{
			foreach (Account acc in accounts)
			{
				if (Acc.Equals(acc))
				{
					return true;
				}
			}
			return false;
		}

		public void add(Account Acc)
		{
			lock(accounts)
				{ accounts.AddLast(Acc); }
		}

		public void updateFrndTL()
		{
			lock(accounts)
			{
				foreach (Account acc in accounts)
				{
					acc.updateFrndTL();
				}
			}
		}

		public void post(string Msg)
		{
			lock (accounts)
			{
				foreach (Account acc in accounts)
				{
					acc.post(Msg);
				}
			}
		}

		#endregion
	}
}
